package com.itheima.reggie.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealDishController {

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;

    @PostMapping
    @Cacheable(value = "setmealCache",key = "#setmealDto.categoryId + '_' + #setmealDto.status")
    public R save(@RequestBody SetmealDto setmealDto) {
        log.info("套餐信息：{}", setmealDto);

        setmealService.addSetmeal(setmealDto);

        return R.success("新增套餐成功");
    }

    @GetMapping("/page")
    public R pageQuery(Integer page, Integer pageSize, String name) {
        log.info("page = {},pagesize = {},name = {}", page, pageSize, name);
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        Page<SetmealDto> dtoPage = new Page<>();
        //模糊查询查询套餐
        LambdaQueryWrapper<Setmeal> qwsetmeal = new LambdaQueryWrapper<>();
        qwsetmeal.like(StringUtils.isNotEmpty(name), Setmeal::getName, name);
        //根据更新时间排序
        qwsetmeal.orderByDesc(Setmeal::getUpdateTime);
        //开始查询
        setmealService.page(pageInfo, qwsetmeal);
        BeanUtils.copyProperties(pageInfo, dtoPage, "records");
        List<Setmeal> records = pageInfo.getRecords();
        List<SetmealDto> list = records.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtil.copyProperties(item, setmealDto);
            Long categoryId = item.getCategoryId();
            //根据菜品分类id查询菜品分类名称
            Category category = categoryService.getById(categoryId);
            if (category != null) {
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
            return setmealDto;
        }).collect(Collectors.toList());
        dtoPage.setRecords(list);

        return R.success(dtoPage);
    }

    @GetMapping("/{id}")
    public R get(@PathVariable Long id) {
        SetmealDto setmealDto = setmealService.getByIdWithDish(id);
        return R.success(setmealDto);
    }


    @PutMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> update(@RequestBody SetmealDto setmealDto) {
        log.info(setmealDto.toString());
        setmealService.updateWithdish(setmealDto);
        return R.success("修改菜品成功");
    }


    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R deleteDish(@RequestParam List<Long> ids) {
        System.out.println(ids);
        setmealService.delete(ids);
        return R.success("删除成功");
    }

    @PostMapping("/status/{status}")
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R updateStatus(@PathVariable Integer status, @RequestParam List<Long> ids) {
        LambdaUpdateWrapper<Setmeal> qwsetmeal = new LambdaUpdateWrapper<>();
        qwsetmeal.set(Setmeal::getStatus, status);
        qwsetmeal.in(Setmeal::getId, ids);
        setmealService.update(qwsetmeal);
        return R.success("操作成功");
    }

    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId + '_' + #setmeal.status")
    public R list(Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> qwsetmeal = new LambdaQueryWrapper<>();
        qwsetmeal.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
        qwsetmeal.eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus());
        qwsetmeal.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(qwsetmeal);
        return R.success(list);
    }

}
